package cn.wolfcode.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.result.JsonResult;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.service.impl.PermissionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model){

        PageInfo<Role> pageInfo = roleService.list(qo);
        model.addAttribute("result",pageInfo);
        return "role/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role) {
        //通过获取参数对象中的ID 来判断是新增还是编辑
        if (role.getId()==null){
            roleService.insert(role);
        }else{
            roleService.updateByPrimaryKey(role);
        }
        return "redirect:/role/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        roleService.deleteByPrimaryKey(id);
        return JsonResult.success(null);
    }

    @RequestMapping("/input")
    public String input(Long id,Model model){
        List<Permission> permissions = permissionService.selectAll();
        model.addAttribute("perList",permissions);
        if (id==null){

        }else{
            Role role = roleService.selectByPrimaryKey(id);
            model.addAttribute("pr",role);
            List<Permission> permissionList = permissionService.selectByEmployeeId(id);
            model.addAttribute("selfPermissions",permissionList);
        }
        return "role/input";

    }
}
